package v3.object.command.list;

import galaxy.obj.Point;

import java.util.Map;

import text.TextUtil;
import v3.object.STcfg;
import v3.object.command.aCommand;
import v3.object.command.iCommand;
import v3.object.command.servoCommand.ComputerMessage;
import v3.object.ship.Ship;
import v3.object.space.Sector;
import v3.object.space.Universe;
import v3.object.space.iSpaceObj;




public class Manuver extends aCommand
{

	private int cursor;
	private double radius;

	public Manuver()
	{

		super();
		commandName="MANUVER";
	}

	@Override
	public void getHelp(Ship ship)
	{

		String help = "Manuver [ma] Cursor Radius\n\n" + "Move the ship using sub-light speed inside the Sector\n" 
				+ "Time Cost " + ship.getTimeConsume((this.getClass())) + "\n" 
				+ "Energy Cost " + ship.getEnergyConsume((this.getClass())) + "";
		help = TextUtil.formatHelp(help);
		System.out.println(help);
	}

	@Override
	protected boolean isCommandAccetable(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return true;
	}

	@Override
	protected double calculateEnergy(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return ship.getEnergyConsume((this.getClass()));
	}

	@Override
	protected double calculateTime(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return ship.getTimeConsume((this.getClass())) ;
	}

	@Override
	protected void checkInput(String[] input) throws Exception
	{
		cursor = Integer.parseInt(input[1]);
		radius = Double.parseDouble(input[2]);
	}

	@Override
	protected boolean realExecute(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		Point pos =  ship.getSectorPosition();
		Point newPos = STcfg.getNewPosition(pos, cursor, radius);
		Sector s = universe.getSector(ship.getPosition());
		iSpaceObj spaceObj= s.getISpaceObject(newPos);
		if(spaceObj == null)
		{
			ship.setSectorPosition(newPos);
			
			Point nsp = universe.getSector(newPos).getFreeSectorPoint();
			if(nsp==null)
			{
				iCommand msg = new ComputerMessage(commandName+": ABORTED - Fluctuation of the Dilithium Crystal stopped the Warp Drives");
				msg.execute(cfg, universe, ship, commands, input);
				ship.setPosition(pos);
			}
			ship.setSectorPosition(nsp);
		
			try
			{
				universe.getSector(newPos).setISpaceObj(nsp, ship);
			}
			catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			iCommand c = new ShowSector();
			c.execute(cfg, universe, ship, commands, input);
			return true;
		}
		else
		{
			iCommand c = new ComputerMessage("***COLLISION*** - HEAVY DAMAGE To the Hull and to the Shields");
			ship.collision(spaceObj);
			return false;
		}
	}

	@Override
	protected boolean confirmCommand(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return true;
	}
	

}
